<?php
/**
 * Created by PhpStorm.
 * User: xufei
 * Date: 2018/5/12
 * Time: 上午10:30
 */

class site_ctl_admin_smallprogram_index extends site_admin_controller{
    /*
     * 轮播图
     * @public
     */
    public function shuffling()
    {
        $this->finder('site_mdl_smallprogram_columns', array(
            'title' => app::get('site')->_('导航菜单'),
            'orderBy' => 'display_order DESC',
            'base_filter' => array(),
            'actions'=>array(
                array(
                    'label' => app::get('site')->_('添加轮播图'),
                    'href' => 'index.php?app=site&ctl=admin_smallprogram_index&act=add',
                    'target' => 'dialog::{frameable:true, title:\''. app::get('site')->_('添加菜单').'\', width:500, height:350}',
                ),
            ),
        ));
    }

    /*
     * 添加轮播图
     * @public
     */
    public function add()
    {
        $base = array(
            'index' => '首页'
        );
        $location = array(
            'banner'=>'上方banner'
        );
        
        $shuffling = array(
            'is_open' => 'false',
            'status' => 0,
        );
        
        $link_type = 'tag';

        $this->pagedata['link_type'] = $link_type;
        $this->pagedata['shuffling'] = $shuffling;
        $this->pagedata['location'] = $location;
        $this->pagedata['base'] = $base;
        $this->display('admin/smallprogram/add_shufflings.html');
    }//End Function

    
    public function edit($id)
    {
        $shuffling = app::get('site')->model('smallprogram_columns')->getRow('*', array('id'=>$id));
        $custom_url = $shuffling['custom_url'];
        
        $goods_id = 0;
        $tag_id = 0;
        $cat_id = 0;
        $self_tag = '';
        if(strstr($custom_url, 'goods_detail')){
            $link_type = 'goods';
            $goods_id = str_replace('goods_detail?id=','',$custom_url);
        }elseif(strstr($custom_url, 'cateCode')){
            $link_type = 'category';
            $cat_id = str_replace('search?cateCode=','',$custom_url);
            $cat_id = preg_replace('/&title=(.+)/', '', $cat_id);
        }elseif(strstr($custom_url, 'tagId')){
            $link_type = 'tag';
            $tag_id = str_replace('search?tagId=','',$custom_url);
            $tag_id = preg_replace('/&title=(.+)/', '', $tag_id);
        }else{
            $link_type = 'selftag';
            $self_tag = explode('&',$custom_url);
            $self_tag = $self_tag[0];
        }
        
        $this->pagedata['goods_id'] = $goods_id;
        $this->pagedata['tag_id'] = $tag_id;
        $this->pagedata['cat_id'] = $cat_id;
        $this->pagedata['link_type'] = $link_type;
        $this->pagedata['shuffling'] = $shuffling;
        $this->pagedata['self_tag'] = $self_tag;
        $this->display('admin/smallprogram/add_shufflings.html');
    }
    
    public function save()
    {
        $this->begin('index.php?app=site&ctl=admin_smallprogram_index&act=shuffling');
        $params = $this->_request->get_post('shuffling');
        $images = $this->_request->get_post('images');
        $custom_url = '';
        if( $_POST['link_type'] == 'goods' ){
            if(! $_POST['goods_id']){
                $this->end(false, '请选择商品');
            }
            $custom_url = 'goods_detail?id='.$_POST['goods_id'];
        }
        if( $_POST['link_type'] == 'category' ){
            if(! $_POST['cat_id']){
                $this->end(false, '请选择分类');
            }
            $custom_url = 'search?cateCode='.$_POST['cat_id'].'&title='.$params['title'];
        }
        if( $_POST['link_type'] == 'tag' ){
            if(! $_POST['tag_id']){
                $this->end(false, '请选择标签');
            }
            $custom_url = 'search?tagId='.$_POST['tag_id'].'&title='.$params['title'];
        }
        if( $_POST['link_type'] == 'selftag' ){
            if(! $_POST['self_tag']){
                $this->end(false, '请输入跳转路径');
            }
            $custom_url = $_POST['self_tag'].'&title='.$params['title'];
        }
        $data['title']=             $params['title'];
        $data['custom_url']=        $custom_url;
        $data['tostr']=             $params['tostr'];
        $data['display_order']=     $params['display_order'];
        $data['base']=              $params['base'];
        $data['location']=          $params['location'];
        $data['images']=            $images;
        $data['is_open']=          $params['is_open']=='true' ? 'true' : 'false';
        if($params['id']){
            $where['id']=          $params['id'];
            app::get('site')->model('smallprogram_columns')->update($data, array('id'=>$where));
            $this->end(true, app::get('site')->_('修改成功'));
        }else{
            if(app::get('site')->model('smallprogram_columns')->insert($data)){
                $this->end(true, app::get('site')->_('添加成功'));
            }else{
                $this->end(false, app::get('site')->_('添加失败'));
            }
        }
    }


}